import { Module, MiddlewareConsumer, RequestMethod } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UserModule } from './modules/user/user.module';
import { RobotModule } from './modules/robot/robot.module';
import { MessageModule } from './modules/message/message.module';
import { CronModule } from './modules/cron/cron.module';
import { PermissionModule } from './modules/permission/permission.module';
import { LogModule } from './modules/log/log.module';
import { TaskModule } from './modules/task/task.module';
import { MessageTemplateModule } from './modules/message-template/message-template.module';
import { ConfigModule } from '@nestjs/config';
import { AuthLoginMiddleware } from './middleware/auth-login.middleware';
import { AuthPermissionMiddleware } from './middleware/auth-permission.middleware';
import { DefaultNamingStrategy } from 'typeorm';
import { CodeModule } from './modules/code/code.module';

@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: process.env.DB_HOST,
      port: parseInt(process.env.DB_PORT, 10) || 3306,
      username: process.env.DB_USERNAME,
      password: process.env.DB_PASSWORD,
      database: process.env.DB_DATABASE,
      entities: [__dirname + '/**/*.entity{.ts,.js}'],
      synchronize: true,
      logging: true,
      namingStrategy: new DefaultNamingStrategy()
    }),
    UserModule,
    RobotModule,
    MessageModule,
    CronModule,
    PermissionModule,
    LogModule,
    TaskModule,
    MessageTemplateModule,
    CodeModule,
  ],
})
export class AppModule {
  configure(consumer: MiddlewareConsumer) {
    // 配置登录认证中间件
    consumer
      .apply(AuthLoginMiddleware)
      .forRoutes(
        { path: 'robot/getRobot', method: RequestMethod.GET },
        { path: 'robot/createRobot', method: RequestMethod.POST },
        { path: 'robot/updataRobot', method: RequestMethod.POST },
        { path: 'robot/removeRobot', method: RequestMethod.POST },
        { path: 'task/getTask', method: RequestMethod.GET },
        { path: 'task/createMessage', method: RequestMethod.POST },
        { path: 'task/createTask', method: RequestMethod.POST },
        { path: 'task/updateTask', method: RequestMethod.POST },
        { path: 'task/removeTask', method: RequestMethod.POST },
        { path: 'log/getLog', method: RequestMethod.GET },
        { path: 'permission/getPermission', method: RequestMethod.GET },
        { path: 'permission/createPermission', method: RequestMethod.POST },
        { path: 'permission/removePermission', method: RequestMethod.POST },
      );

    // 配置权限认证中间件
    consumer
      .apply(AuthPermissionMiddleware)
      .forRoutes(
        { path: 'robot/updataRobot', method: RequestMethod.POST },
        { path: 'robot/removeRobot', method: RequestMethod.POST },
        { path: 'task/getTask', method: RequestMethod.GET },
        { path: 'task/createMessage', method: RequestMethod.POST },
        { path: 'task/createTask', method: RequestMethod.POST },
        { path: 'task/updateTask', method: RequestMethod.POST },
        { path: 'task/removeTask', method: RequestMethod.POST },
        { path: 'log/getLog', method: RequestMethod.GET },
        { path: 'permission/getPermission', method: RequestMethod.GET },
        { path: 'permission/createPermission', method: RequestMethod.POST },
        { path: 'permission/removePermission', method: RequestMethod.POST },
      );
  }
} 